package com.yanqu.road.dao.impl.player;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.ActivityDetail;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class ActivityDetailDaoImpl extends TempDataObjectDao<ActivityDetail> {

    public Map<Integer, ActivityDetail> getActivityDetailMap() {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, ActivityDetail> dataMap = new ConcurrentHashMap<>();
        String sql = "select * from t_u_activity_detail";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(sql);
            rs = call.executeQuery();
            while (rs.next()) {
                ActivityDetail data = parse(rs);
                dataMap.put(data.getActivityId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    public Map<Integer, ActivityDetail> getCrossActivityDetailMap() {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, ActivityDetail> dataMap = new ConcurrentHashMap<>();
        String sql = "select * from t_u_cross_activity_detail";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(sql);
            rs = call.executeQuery();
            while (rs.next()) {
                ActivityDetail data = parseCrossData(rs);
                if (data != null) {
                    data.setNoneOption();
                    dataMap.put(data.getActivityId(), data);
                }
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    @Override
    protected boolean addData(ActivityDetail data) {
        PreparedStatement call = null;
        String sql = "insert into t_u_activity_detail(activityId,autoSendRewardState) values (?,?)";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setInt(i++, data.getActivityId());
            call.setInt(i++, data.getAutoSendRewardState());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    public boolean addCrossData(ActivityDetail data) {
        data.setBeginWriteTime();
        PreparedStatement call = null;
        String sql = "insert into t_u_cross_activity_detail(activityId,autoSendRewardState,autoSendRewardStateUnion) values (?,?,?)";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setInt(i++, data.getActivityId());
            call.setInt(i++, data.getAutoSendRewardState());
            call.setInt(i++, data.getAutoSendRewardStateUnion());
            if (call.executeUpdate() > 0) {
                data.setNoneOption();
                return true;
            } else {
                return false;
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected boolean updateData(ActivityDetail data) {
        PreparedStatement call = null;
        String sql = "update t_u_activity_detail set autoSendRewardState=? where activityId=?";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setInt(i++, data.getAutoSendRewardState());
            call.setInt(i++, data.getActivityId());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    public boolean updateCrossData(ActivityDetail data) {
        data.setBeginWriteTime();
        PreparedStatement call = null;
        String sql = "update t_u_cross_activity_detail set autoSendRewardState=?, autoSendRewardStateUnion=? where activityId=?";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setInt(i++, data.getAutoSendRewardState());
            call.setInt(i++, data.getAutoSendRewardStateUnion());
            call.setInt(i++, data.getActivityId());
            if (call.executeUpdate() > 0) {
                data.setNoneOption();
                return true;
            } else {
                return false;
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected ActivityDetail parseData(ResultSet rs) throws SQLException {
        ActivityDetail data = new ActivityDetail();
        data.setActivityId(rs.getInt("activityId"));
        data.setAutoSendRewardState(rs.getInt("autoSendRewardState"));
        return data;
    }

    protected ActivityDetail parseCrossData(ResultSet rs) throws SQLException {
        ActivityDetail data = new ActivityDetail();
        data.setActivityId(rs.getInt("activityId"));
        data.setAutoSendRewardState(rs.getInt("autoSendRewardState"));
        data.setAutoSendRewardStateUnion(rs.getInt("autoSendRewardStateUnion"));
        return data;
    }
}
